<?php
namespace Client\Controller;

use Zend\View\Model\ViewModel;

class OrganizationController extends InitializeController
{
	protected $_WEEEKS=array(
		"s1"=>"周一",
		"s2"=>"周二",
		"s3"=>"周三",
		"s4"=>"周四",
		"s5"=>"周五",
		"s6"=>"周六",
		"s7"=>"周日",
		"d1"=>"周一",
		"d2"=>"周二",
		"d3"=>"周三",
		"d4"=>"周四",
		"d5"=>"周五",
		"d6"=>"周六",
		"d7"=>"周日"
	);
	
	public function vSellerAction(){
		$params=$this->getEvent()->getRouteMatch()->getParams();
		$org_id=$params['id']?$params['id']:0;
		
		//获取单位信息
		$orgInfo = array();
		$orgInfo = $this->getOrganizationTable()->getInfoById($org_id);
		
		switch ( $orgInfo['org_level'] ) {
			case  '1' :
				$orgInfo['org_level'] = 'A';
				break;
			case  '2' :
				$orgInfo['org_level'] = 'B';
				break;
			case  '3' :
				$orgInfo['org_level'] = 'C';
				break;
			case  '4' :
				$orgInfo['org_level'] = 'D';
				break;
			default:
				$orgInfo['org_level'] = 'A';
		}
		//获取类目商品信息
		$categoryList = array();
		$categoryList = $this->getCategoryTable()->getCateListByOrgId($org_id);
		foreach ($categoryList as $key=> $category){
			$productList = array();
			$productList = $this->getProductTable()->getProListByCateId($category['category_id']);
			$categoryList[$key]['productList'] = $productList;
		}
		return new ViewModel(array(
				'org_id'=>$org_id,
				'orgInfo'=>$orgInfo,
				'category'=>$categoryList,
		));
		
		
	}
	
	/**
     * 店铺首页|商户信息
     * {@inheritDoc}
     * @see \Zend\Mvc\Controller\AbstractActionController::indexAction()
	 */
	public function vMerchantAction(){
		$params=$this->getEvent()->getRouteMatch()->getParams();
		$org_id=$params['id']?$params['id']:0;
		$weekType=""; //单周或双周
		$orgData=array();//商户数据
		$week="";//这里我们要计算明天的时间
		$day="";//这里我们要计算明天的时间 'm月d日
		
		$week = date('N',strtotime('+1 day'));
		
		//获取单位信息
		$orgInfo = array();
		$orgInfo = $this->getOrganizationTable()->getInfoById($org_id);
		$where=array();
		$where['org_id']=$org_id;
		$mealOrders = $this->getMealOrderTable()->getItems($where);
		
		//计算1970年开始的单双周
		$_time=time();
		$_days=floor($_time/(60*60*24));
		$_num=($_days+3)%14+1;
		if($_num>7){
			$weekType = "d";
		}else{//单周
			$weekType ="s";
		}
		
		$j=$week;//初始化明天是周几
		for($i=1,$type=$weekType;$i<=14;){
			for(;$j<=7&&$i<=14;$j++,$i++){
				$plus=$i;
				$orgData[$type.$j]['day']  = date('m月d日',strtotime("+{$plus} days"));
				$orgData[$type.$j]['week'] = $this->_WEEEKS[$type.$j];
				
			}
			if($j>=7 && $i<=14){
				$j=1;
				if($type=="d"){
					$type="s";
				}else{
					$type="d";
				}
			}
		}
		//print_r($mealOrders);die;
		foreach($mealOrders as $meals){
			foreach($meals as $k=>$v){
				if($orgData[$k]&&$v){
					$orgData[$k]['productList'][$meals[type]][$meals['product_id']] = $meals;
				}	
			}
		}
		
		switch ( $orgInfo['org_level'] ) {
			case  '1' :
				$orgInfo['org_level'] = 'A';
				break;
			case  '2' :
				$orgInfo['org_level'] = 'B';
				break;
			case  '3' :
				$orgInfo['org_level'] = 'C';
				break;
			case  '4' :
				$orgInfo['org_level'] = 'D';
				break;
			default:
				$orgInfo['org_level'] = 'A';
		}
		
		return new ViewModel(array(
				'org_id'=>$org_id,
				'orgInfo'=>$orgInfo,
				'category'=>$orgData,
		));
		
	}
    /**
     * 店铺首页|商城信息
     * {@inheritDoc}
     * @see \Zend\Mvc\Controller\AbstractActionController::indexAction()
     */
    public function indexAction(){

    	$params=$this->getEvent()->getRouteMatch()->getParams();
    	$org_id=$params['id']?$params['id']:0;

        //获取单位信息
        $orgInfo = array();
        $orgInfo = $this->getOrganizationTable()->getInfoById($org_id);
        
        switch ( $orgInfo['org_level'] ) {
            case  '1' :
                $orgInfo['org_level'] = 'A';
                break;
            case  '2' :
                $orgInfo['org_level'] = 'B';
                break;
            case  '3' :
                $orgInfo['org_level'] = 'C';
                break;
            case  '4' :
                $orgInfo['org_level'] = 'D';
                break;
            default:
                $orgInfo['org_level'] = 'A';
        }
        //获取类目商品信息
        $categoryList = array();
        $categoryList = $this->getCategoryTable()->getCateListByOrgId($org_id);
        foreach ($categoryList as $key=> $category){
            $productList = array();
            $productList = $this->getProductTable()->getProListByCateId($category['category_id']);
            $categoryList[$key]['productList'] = $productList;
        }
        return new ViewModel(array(
            'org_id'=>$org_id,
            'orgInfo'=>$orgInfo,
            'category'=>$categoryList,
        ));

    }
    /**
     * 加入收藏
     */
    public function collectAction(){
        $request = $this->getRequest();
        $org_id = $request->getPost('orgId');
        
        
//        $verify_is_login = new VerifyHelper();
//         if(!$verify_is_login->isLogin()){
//             $response = array( 
//                 'reason' => '请先登录！',
//                 'result' => false,
//             );
//             die(json_encode($response));
//         }
        //添加收藏
        $favorites = array(
            'user_id'=> $this->user_id?$this->user_id:123,
            'org_id' => $org_id,
        );
        $has_favorites = $this->getFavoritesTable()->getFavorites($favorites);
        if(count($has_favorites) == 0){
            $result = $this->getFavoritesTable()->create($favorites);
            if($result){
                $response = array(
                    'reason' => '收藏成功！',
                    'result' => true,
                );
            }else{
                $response = array(
                    'reason' => '收藏失败！',
                    'result' => false,
                );
            }
        }else{
            $response = array(
                'reason' => '单位已经收藏！',
                'result' => true,
            );
        }
        die(json_encode($response));
    }
    /**
     * 获取商品原材料
     */
    public function materialAction(){
    
        $request = $this->getRequest();
        $product_id = $request->getPost('product_id');
        if(empty($product_id)){
            $response = array(
                'reason' => '参数错误！',
                'result' => false,
            );
            die(json_encode($response));
        }
        //获取原材料
        $meterial = array();
        $meterial = $this->getMaterialTable()->getMaterialByProductID($product_id);
        $response = array(
            'data' => $meterial,
            'result' => true,
        );
        die(json_encode($response));
    }

}